﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.Msg;
using Microhard.Plat.CRM.Stores.Interface;

namespace Microhard.Plat.CRM.Stores.Validators
{
    public class IspItemModelValidator : AbstractValidator<IspItemModel>
    {
     public IspItemModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(IspItemMsg.NeedName).When(r => r.CheckListItemID == Guid.Empty);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(IspItemMsg.MaxNameLength).When(r => r.CheckListItemID == Guid.Empty);
                Custom(u =>
                {
                    IIspItem iclt = IoC.Resolve<IIspItem>();
                    if (iclt.IsNameDuplicate(u))
                    {
                        return new ValidationFailure("Name", IspItemMsg.DuplicateName);
                    }
                    return null;
                });
            });
        }
    }
}
